//複数検索画面対象名
var gMutiObj = null;
//検索画面から戻す値の処理
var gPara = "";
$(function() {
	$('.btn_search').click(function(){
		var theValue = "";
		var urlPara = makeSearchURL( this.id, theValue );
		$("#dialog").load( urlPara ).dialog({
			modal:true
			}); 
		$('#dialog').dialog('open');
	});
	
	$('.btn_clear').click(function(){
		var cid   =  $(this).attr("id");
		cid = cid.split("_")[0];
		var cd    = cid + "_cd";
		var name  = cid + "_name";
		if ($('#fm [name='+ name + ']').length > 0){  
			$('#fm [name='+ name + ']').attr("value", "");
		} 
		if ($('#fm [name='+ cd + ']').length > 0){  
			$('#fm [name='+ cd + ']').attr("value", "");
		} 
	});
	//event.callback.aClick['li-del'] = function(a){ return li_del(a); };
	$('.btn_search_muti_ins').click(function(){
		var theValue = "";
		gMutiObj = this.id;
		var urlPara = makeMutiSearchURL( gMutiObj, theValue );
		
		$("#dialogmutiins").load( urlPara ).dialog({
			modal:true
			}); 
		$('#dialogmutiins').dialog('open');
		//iii = iii + 1;
		//li_add(this.id,iii, "inVal")
	});
	
	$('.btn_search_muti').click(function(){
		var theValue = "";
		gMutiObj = this.id;
		var urlPara = makeMutiSearchURL( gMutiObj, theValue );
		$("#dialogmuti").load( urlPara ).dialog({
			modal:true
			}); 
		$('#dialogmuti').dialog('open');
		//iii = iii + 1;
		//li_add(this.id,iii, "inVal")
	});
	$('.btn_search_mutidnp').click(function(){
		var theValue = "";
		gMutiObj = this.id;
		var urlPara = makeURL( gMutiObj, theValue, "mserach_muti_dnp_shop.php");
		 //makeURL(inval, innameval, "mserach_muti.php") 
		$("#dialogmuti").load( urlPara ).dialog({
			modal:true
			}); 
		$('#dialogmuti').dialog('open');
		//iii = iii + 1;
		//li_add(this.id,iii, "inVal")
	});

	
	
	$('#dialog').dialog({
		    autoOpen: false,
		    width: 380,
		    height:380,
		    title: '検索',
		    modal:true,
		    resizable:false,
		    buttons: {
		    "キャンセル": function() {
		    $('#dialog').innerHTML ="";
		   	 $(this).dialog("close");
		    }
		 }
	 });
	
	$('#dialogmuti').dialog({
	    autoOpen: false,
	    width: 700,
	    height:390,
	    title: '検索',
	    modal:true,
	    resizable:false,
	    buttons: {
	    "キャンセル": function() {
	    	$('#dialogmuti').innerHTML ="";
	   	 $(this).dialog("close");
	    },
       "確定": function() {
    	   if (typeof(g_shop_ul_to_grid)!="undefined") {
    		   copy_shop_pop_ul_to_grid();
    	   } else {
    		   copy_pop_ul();  
    	   }
    	  $('#dialogmuti').innerHTML ="";
    	  
	     $(this).dialog("close");
	    }
	 }
	});
	
	$('#dialogmutidnpw').dialog({
	    autoOpen: false,
	    width: 600,
	    height:390,
	    title: '検索',
	    modal:true,
	    resizable:false,
	    open: function(event) {
    		if (add_del_shop_flg == "削除") {
	    		var button = $('.ui-dialog-buttonpane').find('button:contains("一括追加")');
	    		button.attr('disabled','disabled');
	    		button = $('.ui-dialog-buttonpane').find('button:contains("一括削除")');
	    		button.removeAttr("disabled");
    		} else {
	    		var button = $('.ui-dialog-buttonpane').find('button:contains("一括削除")');
	    		button.attr('disabled','disabled');
	    		button = $('.ui-dialog-buttonpane').find('button:contains("一括追加")');
	    		button.removeAttr("disabled");
    		}
	    },
	    buttons: {
       "一括追加": function() {
    	 addAllShopTd();
	    },
	    	"一括削除": function() {
    	   delAllShopTd();
	    },
	    "キャンセル": function() {
	    	$('#dialogmuti').innerHTML ="";
		   	 $(this).dialog("close");
		    }
	 }
	});

	$('#dialogmutiins').dialog({
	    autoOpen: false,
	    width: 900,
	    height:390,
	    title: '検索',
	    modal:true,
	    resizable:false,
	    buttons: {
	    "キャンセル": function() {
	    	$('#dialogmutiins').innerHTML ="";
	   	 $(this).dialog("close");
	    },
       "確定": function() {
    	   if (typeof(g_shop_ul_to_grid)!="undefined") {
    		   copy_shop_pop_ul_to_grid();
    	   } else {
    		   copy_pop_ul();  
    	   }
    	  $('#dialogmutiins').innerHTML ="";
	     $(this).dialog("close");
	    }
	 }
	});
	$('.li_del').live('click',function(){
		$(this).parent().remove();
	});
});


function makeSearchURL(inval, innameval) {
	//inval -> media,delive, shop 
	var  sphp = "mserach_simple.php";
	if  (inval  == "shop") {
		sphp = "mserach_shop.php";
	}
	gPara = inval;
	var tpara = inval;
	/*
	if (inval.substr(0,5)  == "lnews") {
		tpara = "mediaid";
	} 
	 */
	var urlPara = sphp + "?t=" + tpara ;
	urlPara = urlPara + "&v="+ encodeURI(innameval);
	var myDate = new Date();
	urlPara = urlPara + "&s=" + myDate.getTime(); 
	return urlPara;
}

function makeMutiSearchURL(inval, innameval) {
	gPara = inval;
	return  makeURL(inval, innameval, "mserach_muti.php") 
	/*
	var urlPara = "mserach_muti.php?t=" + inval ;
	urlPara = urlPara + "&v="+ encodeURI(innameval);
	var myDate = new Date();
	urlPara = urlPara + "&s=" + myDate.getTime(); 
	return urlPara;
	*/
}
function makeURL(inval, innameval,inphpfile) {
	gPara = inval;
	var urlPara = inphpfile + "?t=" + inval ;
	urlPara = urlPara + "&v="+ encodeURI(innameval);
	var myDate = new Date();
	urlPara = urlPara + "&s=" + myDate.getTime(); 
	return urlPara;
}

function copy_pop_ul() {
	$("#" + gMutiObj +"_ul").append($("#pop_ul").get(0).innerHTML);
}

function checkNames(inval) {
	var retFlg = true;
	var obj = gMutiObj;
	if (gMutiObj == "ins_shop_patt") {
		obj =  "ins_shop";
	}
	var names = document.getElementsByName(obj + '_cd[]');
	//$.each($("input:hidden"), function(i,val){
	$.each(names, function(i,val){
		if (inval == val.value) {
	   	 retFlg = false;
	   	 alert("既に選択された項目です");
	   	 return false;
	   	}
	}); 
		
	if (typeof(g_shop_ul_to_grid)!="undefined") {
		var namesul = document.getElementsByName(obj + '_cd_ul[]');
		//$.each($("input:hidden"), function(i,val){
			$.each(namesul, function(i,val){
		     if (inval == val.value) {
		    	 retFlg = false;
		    	 alert("既に選択された項目です");
		    	 return false;
		     	}
			}); 
			
	}
	return retFlg;
}